package com.xiaow.springsecuriydemo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaow.springsecuriydemo.entity.Shop;
import com.xiaow.springsecuriydemo.service.ShopService;
import com.xiaow.springsecuriydemo.utils.JwtUtils;
import com.xiaow.springsecuriydemo.vo.Result;
import io.jsonwebtoken.Claims;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xiaow
 * @since 2022-09-17
 */
@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    ShopService shopService;

    @GetMapping("/getByUserId")
    @PreAuthorize("hasAuthority('/shoper')")
    public Result getByUserId(HttpServletRequest httpServletRequest) {
        String token = httpServletRequest.getHeader("token");
        if (Strings.isEmpty(token)) {
            return Result.result("请先登录", 401, "");
        }
        Claims claims = JwtUtils.getClaims(token);
        String userid = (String) claims.get("userid");
        QueryWrapper<Shop> shopQueryWrapper = new QueryWrapper<>();
        shopQueryWrapper.eq("userid", Integer.parseInt(userid));
        List<Shop> list = shopService.list(shopQueryWrapper);
        return Result.succ(list);
    }


    @GetMapping("/getShopInfoByShopId")
    public Result getShopInfoByShopId(Integer shopid) {
        Shop byId = shopService.getById(shopid);
        return Result.succ(byId);
    }


}
